<?php


namespace app\admin\controller;

use app\api\service\QiWechat as QI;
use app\common\controller\AdminBaseController;
use app\common\model\tang\FormatsConfig;
use app\common\model\tang\MerchantMobile;
use app\common\model\tang\Merchant;
use app\common\model\tang\MallActivity as tice;
use app\common\model\tang\NoticeLog;
use app\common\model\tang\ReleaseUser;
use app\common\model\tang\WechatQi;
use app\common\model\tang\NoticeApply;
use think\db\Where;

class MallActivity extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new tice();
    }

    /**
     * @return string
     * @throws \Exception  首页
     */
    public function index()
    {
        if (IS_AJAX) {
            return $this->model->getAdminPageData($this->param, 'id desc');
        }
        return $this->fetch();
    }

    /**
     * 添加发布内容和修改内容
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $data['is_apply'] = 1; //不需要报名
            //send_who 1 全体商户 2楼层 3 业态
            $where=[];
            if(!empty($data['lcOrgid'])){
                $lcOrgid = $data['lcOrgid'];
                $where[]=['lcName','=',$lcOrgid];
            }
            if(!empty($data['organization'])){
                $organization = $data['organization'];
                $where[]=['organization','=',$organization];
            }
            if(!empty($data['organization_one'])){
                $organization_one = $data['organization_one'];
                $where[]=['organization_one','=',$organization_one];
            }
            $send_sum = Merchant::where('biz_state','effect')->where($where)->count();
            if(!empty($data['merchant_list'])){
                $send = explode(',',$data['merchant_list']);
                $send_sum = count($send);
            }else{
                $merchant_sum = Merchant::where($where)->where('biz_state','effect')->column('id');
                $data['merchant_list'] = implode(',',$merchant_sum);
            }
            $read_sum = 0;
            if(isset($data['id'])){
                $read = (new NoticeApply())->where('noticeId',$data['id'])->count();
                $noread = $send_sum-$read;
                $read_sum =$read;
            }else{
                $noread = $send_sum;
            }
            $data['send_sum'] = $send_sum;
            $data['noread_sum'] = $noread;
            $data['read_sum'] = $read_sum;
            return $this->model->doAll($data);
        }
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);
        $lcInfo = (new \app\common\model\tang\FloorConfig())->select();
        $orange = FormatsConfig::where('pid',0)->select();
        if(!empty($info['organization'])){
            $orange_one = FormatsConfig::where('pid',$info['organization'])->select();
            $this->assign(compact("orange_one", "id"));
        }
        if(!empty($info['merchant_list'])){
            $mer = explode(',',$info['merchant_list']);
            $merchant_list=[];
            foreach ($mer as $k=>$v){
                $temp['id'] = $v;
                $mers = (new Merchant())->where(['id'=>$v])->field('name,positions_code')->find();
                $temp['name'] =$mers['name'].$mers['positions_code'];
                $merchant_list[] = $temp;
            }
            $this->assign(compact("merchant_list"));
        }
        if(!empty($info['content'])){
            $info['content'] = html_entity_decode($info['content'], ENT_QUOTES);
        }
        $this->assign(compact("info", "id","lcInfo",'orange'));

        return $this->fetch();
    }
    public function getMerchantList(){
        $param = $this->request->post();
        $where=[];
        if(isset($param['lcOrgid'])){
            $lcOrgid = $param['lcOrgid'];
            $where[]=['lcName','=',$lcOrgid];
        }
        if(isset($param['organization'])){
            $organization = $param['organization'];
            $where[]=['organization','=',$organization];
        }
        if(isset($param['organization_one'])){
            $organization_one = $param['organization_one'];
            $where[]=['organization_one','=',$organization_one];
        }
        $mercahnt= (new Merchant())->where('biz_state','effect')->where($where)->column('id,name,positions_code');
        return $mercahnt;
    }
    /**
     * 选择业态下拉框
     */
    public function change(){
        $pid = $this->request->param('pid');
        $orange = FormatsConfig::where('pid',$pid)->field('id,name')->select()->toArray();
        return self::ajaxResult($orange);
    }
    /**
     * 查看未读用户
     */
    public function noRead(){
        $param = $this->param;
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id)->toArray();
        $where = [];
        if(empty($info['merchant_list'])){
            if(!empty($info['lcOrgid'])){
                $lcOrgid = $info['lcOrgid'];
                $where[]=['lcOrgid','=',$lcOrgid];
            }
            if(!empty($info['organization'])){
                $organization = $info['organization'];
                $where[]=['organization','=',$organization];
            }
            if(!empty($info['organization_one'])){
                $organization_one = $info['organization_one'];
                $where[]=['organization_one','=',$organization_one];
            }
        }else{
            $where[]=['id','in',$info['merchant_list']];
        }
        $allMerchant = Merchant::field('id,name,mobile')->where('status',1)->where($where)->select()->toArray();
        $redPerson = \app\common\model\tang\NoticeApply::where('noticeId',$param['id'])->select()->toArray();
        foreach ($allMerchant as $k=>$v){
            foreach ($redPerson as $kk=>$vv){
                if($v['id']==$vv['userId']){
                    unset($allMerchant[$k]);
                }
            }
        }
        $this->assign('type','未读商户');
        $this->assign('list',$allMerchant);
        return $this->fetch();
    }
    /**
     *查看发布的内容
     */
    public function see()
    {
        $param = $this->param;
        if (IS_AJAX) {
            dd($this->param);
        }
        $result = tice::where('id', '=', $param['id'])->find()->toArray();
        $result['content'] = html_entity_decode($result['content'], ENT_QUOTES);
        $this->assign('info', $result);
        return $this->fetch();
    }

    /**
     * @return mixed|void删除
     */
    public function del()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");
            $result = tice::where('id', 'in', $ids)->delete();
            if ($result) {
                return self::JsonReturn("删除成功");
            } else {
                return self::JsonReturn("删除失败");
            }

        }
    }
}